<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


class LetterTemplateView_form extends AppView
{
	
	var $template = 'form.html';
	
	function execute(&$obj, &$manager) {
		
		$this->addMsg('common_msg.ini', 'email_setting');
		
		$tpl = &new tplTemplatez($this->template_dir . $this->template);
		$tpl->tplAssign('error_msg', AppMsg::errorBox($obj->errors));
		
		// template tags
		$p = new AppMailParser();
		$tags = '[' . implode('], [', $p->getVars()) . ']';
		$msgs = array('title'=>$this->msg['template_tags'], 'body'=>$tags);
		$tpl->tplAssign('error_msg', BoxMsg::factory('hint', $msgs));
		
		
		if($obj->get('is_html')) {
			$tpl->tplAssign('vck_editor', $this->getHtmlArea($obj->get('body')));
		} else {
			$tpl->tplAssign('vck_editor', $this->getTextArea($obj->get('body')));
		}
		
		if($obj->get('predifined')) {
			$tpl->tplSetNeeded('/title_text');
			$tpl->tplSetNeeded('/description_text');
		} else {
			$tpl->tplSetNeeded('/title_input');
			$tpl->tplSetNeeded('/description_input');		
		}
		
		
		$skip_block = array('from', 'to', 'to_cc', 'to_bcc');
		$skip = explode(',', $obj->get('skip_field'));
		foreach($skip_block as $k => $v) {
			if(!in_array($v, $skip)) {
				$tpl->tplSetNeeded('/' . $v);
			} else {
				$obj->hidden[] = $v;
			}
		}
		
		
		$tpl->tplAssign($this->setCommonFormVars($obj));
		$tpl->tplAssign($this->setStatusFormVars($obj->get('active')));
		$tpl->tplAssign($obj->get());
		$tpl->tplAssign($this->msg);
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getHtmlArea(&$value) {
		
		require_once APP_ROOT_DIR . 'tools/FCKeditor/fckeditor.php';
		
		$oFCKeditor = new FCKeditor('body');
		
		if(!$oFCKeditor->IsCompatible()) {
			$this->controller->go('editor_incompatible', true);
		}
		
		$oFCKeditor->BasePath = APP_HOME_PATH . 'tools/FCKeditor/';
		$oFCKeditor->ToolbarSet = 'Default';
		$oFCKeditor->ToolbarSet = 'LetterTemplate';
		$oFCKeditor->Width  = '100%';
		$oFCKeditor->Height = '300';
		$oFCKeditor->Value	= &$value;
		
		return $oFCKeditor->CreateHtml();	
	}
	
	
	function getTextArea($value) {
		$str = '<textarea cols="60" rows="20" name="body" style="width: %s;">%s</textarea>';
		return sprintf($str, '100%', $value);
	}
}
?>